package com.th.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.th.common.exception.base.BaseServiceException;
import com.th.system.domain.UsTask;
import com.th.system.mapper.UsTaskMapper;
import com.th.system.service.UsTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ValcanoZz
 * @creat 2023-06-23-9:15
 */

@Service
public class UsTaskServiceImpl extends ServiceImpl<UsTaskMapper, UsTask> implements UsTaskService {

    @Autowired
    private UsTaskMapper usTaskMapper;

    @Override
    public Long insertUsTask(UsTask usTask) {
        if (!this.save(usTask)) {
            throw new BaseServiceException(500, "当前任务提交失败");
        } else {
            return usTask.getId();
        }
    }

    @Override
    public List<UsTask> selectTasksByOrderId(Long orderId) {
        return usTaskMapper.selectList(new LambdaQueryWrapper<UsTask>().eq(UsTask::getOrderId, orderId));
    }

    @Override
    public List<UsTask> selectPage(int startPage, int size, LambdaQueryWrapper<UsTask> wrapper) {
        Page<UsTask> page = usTaskMapper.selectPage(new Page<>(startPage, size), wrapper);
        return page.getRecords();
    }

    @Override
    public void updateTask(UsTask usTask) {
        if (usTaskMapper.updateById(usTask) <= 0) {
            throw new BaseServiceException(500, "任务更新失败,id为" + usTask.getId());
        }
    }

    @Override
    public void deleteTask(Long id) {
        if (usTaskMapper.selectById(id) == null){
            return;
        }
        if (usTaskMapper.deleteById(id) <= 0) {
            throw new BaseServiceException(500, "任务删除失败,id为" + id);
        }
    }
}
